import { plugin, verc, config } from '../../api/api.js';
import {
  Read_player,
  existplayer,
  Read_najie,
  Write_lingzhuang,
  Add_灵石,
  __PATH,
  Go,
  get_najie_img,
  get_najie_chouchou_img,Read_lingzhuang,
  channel
} from '../../model/xiuxian.js';
export class UserAction extends plugin {
  constructor() {
    super({
      name: 'UserAction',
      dsc: '交易模块',
      event: 'message',
      priority: 600,
      rule: [
        {
          reg: '^我的纳戒$',
          fnc: 'Show_najie',
        },
        {
          reg: '^我的灵庄$',
          fnc: 'Show_lingzhuang',
        },
        {
          reg: '^瞅瞅纳戒$',
          fnc: 'Show_najie_luck',
        },
        {
          reg: '^升级灵庄$',
          fnc: 'Lv_up_lingzhuang',
        },
        {
          reg: '^(存|取)灵石(.*)$',
          fnc: 'Take_lingshi',
        },
        {
          reg: '^灵庄结算$',
          fnc: 'Lingzhuang_settlement',
        }
      ],
    });
  }

  async Show_najie_luck(e){
    
    
    e.reply("敬请期待");
    return false;
  }
  async Show_lingzhuang(e) {
    
    let usr_qq = e.user_id.toString().replace('qg_','');
    usr_qq = await channel(usr_qq);
    //有无存档
    let ifexistplay = await existplayer(usr_qq);
    if (!ifexistplay) return false;
    let lingzhuang = await Read_lingzhuang(usr_qq);
    // let 灵庄等级 = lingzhuang.level;
    // let 已存灵石 = lingzhuang.lingshi;
    // let 最大存储 = lingzhuang.max_lingshi;
    
    e.reply(`你的灵庄\n等级为${lingzhuang.level}\n已存${lingzhuang.lingshi}/${lingzhuang.max_lingshi}灵石`);
    return false;
  }
  

  //我的纳戒
  async Show_najie(e) {
    
    let usr_qq = e.user_id.toString().replace('qg_','');
    usr_qq = await channel(usr_qq);
    //有无存档
    let ifexistplay = await existplayer(usr_qq);
    if (!ifexistplay) return false;
    let img = await get_najie_img(e);
    e.reply(img);
    return false;
  }

  //灵庄升级
  async Lv_up_lingzhuang(e) {
    
    let flag = await Go(e);
    if (!flag) return false;
    let usr_qq = e.user_id.toString().replace('qg_','');
    usr_qq = await channel(usr_qq);
    //有无存档
    let ifexistplay = await existplayer(usr_qq);
    if (!ifexistplay) return false;
    let lingzhuang = await Read_lingzhuang(usr_qq);
    let player = await Read_player(usr_qq);
    const cf = config.getConfig('xiuxian', 'xiuxian');
    let lingzhuang_num = cf.lingzhuang_num;
    let lingzhuang_price = cf.lingzhuang_price;
    if (lingzhuang.level == lingzhuang_num.length) {
      e.reply('你的灵庄已经是最高级的了');
      return false;
    }
    if (player.灵石 < lingzhuang_price[lingzhuang.level]) {
      e.reply(
        `灵石不足,还需要准备${lingzhuang_price[lingzhuang.level] - player.灵石}灵石`
      );
      return false;
    }
    await Add_灵石(usr_qq, -lingzhuang_price[lingzhuang.level]);
    lingzhuang.max_lingshi = lingzhuang_num[lingzhuang.level];
    lingzhuang.level += 1;
    await Write_lingzhuang(usr_qq, lingzhuang);
    e.reply(
      `你的灵庄升级成功,花了${
        lingzhuang_price[lingzhuang.level - 1]
      }灵石,目前灵庄灵石存储上限为${lingzhuang.max_lingshi},可以使用【我的灵庄】来查看`
    );
    return false;
  }
  //存取灵石
    async Take_lingshi(e) {
      
      let usr_qq = e.user_id.toString().replace('qg_','');
      usr_qq = await channel(usr_qq);
      let ifexistplay = await existplayer(usr_qq);
      if (!ifexistplay) return false;
      let flag = await Go(e);
      if (!flag) return false;
      //检索方法
      var reg = new RegExp(/取|存/);
      let func = reg.exec(e.msg);
      if (!func) {
        e.reply('命令格式错误，请输入【存灵石数量】或【取灵石数量】');
        return false;
      }
      let msg = e.msg.replace(reg, '');
      msg = msg.replace('', '');
      let lingshi = msg.replace('灵石', '');
      if (func == '存' && lingshi == '全部') {
        let P = await Read_player(usr_qq);
        if (!P || !P.灵石) {
          e.reply('无法读取玩家灵石信息');
          return false;
        }
        lingshi = P.灵石;
      }
      if (func == '取' && lingshi == '全部') {
        let N = await Read_lingzhuang(usr_qq);
        if (!N || !N.灵石) {
          e.reply('无法读取灵庄灵石信息');
          return false;
        }
        lingshi = N.灵石;
      }
      if (isNaN(lingshi) || lingshi <= 0) {
        e.reply('灵石数量必须为正整数');
        return false;
      }
      lingshi = await convert2integer(lingshi);
      if (func == '存') {
        let player_lingshi = await Read_player(usr_qq);
        player_lingshi = player_lingshi.灵石;
        if (player_lingshi < lingshi) {
          e.reply([
            segment.at(usr_qq),
            `灵石不足,你目前只有${player_lingshi}灵石`,
          ]);
          return false;
        }
        let lingzhuang = await Read_lingzhuang(usr_qq);
        if (lingzhuang.max_lingshi < lingzhuang.lingshi + lingshi) {
        let can_store = lingzhuang.max_lingshi - lingzhuang.lingshi;
        if (lingzhuang.max_lingshi <= lingzhuang.lingshi) {
          e.reply('灵庄已满，无法存入灵石');
          return false;
        }
        await Write_lingzhuang(usr_qq, {
          ...lingzhuang,
          lingshi: lingzhuang.max_lingshi
        });
        await Add_灵石(usr_qq, -can_store);
          e.reply([
            `已为您放入${can_store}灵石,灵庄存满了`,
          ]);
          return false;
        }
        lingzhuang.lingshi += lingshi;
        await Write_lingzhuang(usr_qq, lingzhuang);
        await Add_灵石(usr_qq, -lingshi);
        e.reply([
          `储存完毕,你目前还有${player_lingshi - lingshi}灵石,灵庄内有${lingzhuang.lingshi
          }灵石`,
        ]);
        return false;
      }
      if (func == '取') {
        let lingzhuang = await Read_lingzhuang(usr_qq);
      if (lingzhuang.lingshi < lingshi) {
          e.reply([
            `灵庄灵石不足,你目前最多取出${lingzhuang.lingshi}灵石`,
          ]);
          return false;
        }
        let player_lingshi = await Read_player(usr_qq);
        player_lingshi = player_lingshi.灵石;
        lingzhuang.lingshi -= lingshi;
        await Write_lingzhuang(usr_qq, lingzhuang);
        
        await Add_灵石(usr_qq, lingshi);
        e.reply([
          `本次取出灵石${lingshi},你的灵庄还剩余${lingzhuang.lingshi}灵石`,
        ]);
        return false;
      }
      return false;
    }
  

  //灵庄结算
  async Lingzhuang_settlement(e) {
    let usr_qq = e.user_id.toString().replace('qg_','');
    usr_qq = await channel(usr_qq);
    
    //有无存档
    let ifexistplay = await existplayer(usr_qq);
    if (!ifexistplay) return false;
    let lingzhuang = await Read_lingzhuang(usr_qq);
    const cf = config.getConfig('xiuxian', 'xiuxian');
    let interest_rate = cf.interest_rate || 0.1; // 默认每日利率为1%
    console.log(`利率: ${interest_rate}`);
    let deposit_time = lingzhuang.deposit_time || Date.now();
    let minutes = (Date.now() - deposit_time) / (1000 * 60);
    let interest = Math.floor(lingzhuang.lingshi * interest_rate * (minutes / 1440)); // 1440 minutes = 1 day
    await Add_灵石(usr_qq, interest);
    lingzhuang.deposit_time = Date.now();
    await Write_lingzhuang(usr_qq, lingzhuang);
    let player = await Read_player(usr_qq);
    e.reply(`灵庄结算成功，获得利息${interest}灵石，当前拥有${player.灵石}灵石`);
    return false;
  }
}
/**
 * 
 * @param {*} amount 输入数量
 * @returns 返回正整数
 */
export async function convert2integer(amount) {
  let number = 1;
  let reg = new RegExp(/^[1-9][0-9]{0,12}$/);
  if (!reg.test(amount)) {
    return number;
  } else {
    return parseInt(amount);
  }
}